<?php

namespace admin\models;

use common\models\SearchInterface;
use common\models\SearchTrait;
use Yii;
use common\models\BookChapter;

/**
 * BookChapterSearch represents the model behind the search form of `common\models\BookChapter`.
 */
class BookChapterSearch extends BookChapter implements SearchInterface
{
    use SearchTrait;
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title'], 'trim'],
            [['book_id'], 'integer'],
            [['display_order'], 'string'],
        ];
    }

    public function prepareQuery($query)
    {
        //排序
        $order = 'display_order asc';
        if ($this->display_order == 'desc') {
            $order = 'display_order desc';
        }
        return $query->andFilterWhere(['book_id' => $this->book_id])
            ->andFilterWhere(['like', 'title', $this->title])
            ->orderBy($order);
    }
}
